﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using Newtonsoft.Json;
using System.Web.Security;
using ZLCMS.Business.Util;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Common;

namespace ZLCMS.Web.SystemManage.Control.FileControl
{
    public partial class Upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // firefox、chrome不会把验证信息回传，因此需要特殊处理验证
            if (!String.IsNullOrEmpty(Request["ajax"]))
            {
                FormsAuthenticationTicket userTicket;

                try
                {
                    if (!String.IsNullOrEmpty(Request["User"]))
                    {
                        userTicket =FormsAuthentication.Decrypt(Request["User"]);

                        if (userTicket.Expired)
                        {
                            throw new Exception("用户凭证已过期!");
                        }
                    }
                    else
                    {
                        throw new Exception("请求不包含用户凭证信息!");
                    }

                    UploadHandle handle = new UploadHandle(Page) { UploadMen = userTicket.Name};
                    handle.SaveUploadFile();

                    SimpleFileInfo simple = new SimpleFileInfo();
                    simple.FileId = handle.File.Id;
                    simple.FileName = handle.File.FileName;
                    simple.FilePath = handle.File.FilePath;
                    simple.FileExt = simple.FilePath.Substring(simple.FilePath.LastIndexOf('.') + 1).ToLower();

                    String response = "{\"stat\":0,\"file\":" + JsonConvert.SerializeObject(handle.File) + ",\"simple\":" + JsonConvert.SerializeObject(simple) + "}";
                    Response.Write(response);
                }
                catch (System.Exception ex)
                {
                    Response.Write("{\"stat\":1,\"msg\":\"" + ex.Message + "\"}");
                }
                Response.End();
                return;
            }
            else
            {
                //验证登录
                if (UserKit.GetUserAccountFailLogin() == null)
                {
                    new FrameHelper(Page).Alert(new ReturnCode() { statusCode = 301, message = "用户凭证已过期!" });
                    return;
                }
            }
        }

        protected String GetIdentityStr()
        {           
            FormsIdentity cIdentity = User.Identity as FormsIdentity;
            if (cIdentity != null)
            {
                return FormsAuthentication.Encrypt(cIdentity.Ticket);
            }
            return "";
        }

//         protected String GetFolder()
//         {
//             // 创建时有传入SESSION，因此可以使用UserKit.GetUserAccount()
//             try
//             {
//                 return ParamKit.GetValue("SystemConfig", "FileUploadPath", "/All/").Replace("%UID%", UserKit.GetUserAccount());
//             }
//             catch (System.Exception ex)
//             {
//                 new FrameHelper(Page).Alert(new ReturnCode() { statusCode = 301, message = "用户凭证已过期!" });
//                 return null;
//             }
//         }

        protected String GetSizeLimit()
        {
            return Request["limit"];
        }

        protected String IsMulti()
        {
            bool mul = GetSizeLimit() != "1";
            return mul.ToString();
        }

        protected string jsonString(string str)
        {
            str = str.Replace("\\", "\\\\");
            str = str.Replace("/", "\\/");
            str = str.Replace("'", "\\'");
            return str;
        }

       protected string GetFileExt()
       {
           if (string.IsNullOrEmpty(Request["ext"]))
           {
               return "*.*";
           }

           string extString = "";
           string[] extList = Request["ext"].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

           foreach (string item in extList)
           {
               extString += "*." + item + ";";
           }
           return extString;
       }
    }
}
